package com.ce.pms.system.occupation.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;

import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.base.vo.SearchVO;
import com.ce.pms.system.occupation.dao.IThingHandoverCongfigDao;
import com.ce.pms.system.occupation.model.ThingHandoverCongfigDO;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;
/**
 * 功能描述: 移交物品设置Dao功能实现类
 * <p>版权所有：中企动力
 * <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author  胡煦
 * Create on:2008-12-29
 * Edit author:
 * Edit on:
 * 修改原因：

 */

public class ThingHandoverCongfigDaoImpl extends PmsBaseDaoImpl<ThingHandoverCongfigDO> implements
		IThingHandoverCongfigDao {

	@Override
	public ThingHandoverCongfigDO findUniqueByName(String thingHandoverCongfigName)
	{
		super.setNameKey("thingHandoverCongfigName");
		return super.findUniqueByName(thingHandoverCongfigName);
	}
	/**
	 * 方法描述: 分页查询所有移交物品设置
	 * @author 胡煦
	 * Create on:2008-12-29
	 * @param searchVO
	 * @return 返回当前页的查询结果
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public Page queryAllThingHandoverCongfigByPage(SearchVO searchVO){
		if(!BlankUtil.isBlank(searchVO.getOrderKey())&&!BlankUtil.isBlank(searchVO.getOrderValue())){
			return super.queryByPage(searchVO);
		}else{
			return this.getThingHandoverCongfigList(searchVO);
		}
	}
	/**
	 * 方法描述: 移交物品设置列表查询方法(根据序号升序排序，如果为空则在最后)
	 * @author 胡煦
	 * Create on:2008-12-29
	 * @param name
	 * @return Page
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public Page getThingHandoverCongfigList(SearchVO searchVO){

		String queryStr = "SELECT * FROM `OM_THING_HANDOVER_CONGFIG` hgr ORDER BY IF(hgr.`SEQUENCE` is NULL,(SELECT max(hgr1.`SEQUENCE`) FROM `OM_THING_HANDOVER_CONGFIG` hgr1)+1,hgr.`SEQUENCE`)";
		Page page = null;
		if(BlankUtil.isBlank(searchVO)){
			Query query = this.createSQLQuery(queryStr);
			page = new Page();
			page.setResult(this.changeObjToDo(query.list()));

		}else{
			page = this.pagedSqlQueryByPageNo(queryStr, searchVO.getPageNo(), searchVO.getPageSize());
			List<ThingHandoverCongfigDO> list = this.changeObjToDo(page.getResult());
			page.setResult(list);
		}
		return page;
	}

	/**
	 * 方法描述: Object To Domain
	 * @author 胡煦
	 * Create on:2008-12-29
	 * @param List<Object[]>
	 * @return List<ThingHandoverCongfigDO>
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	private List<ThingHandoverCongfigDO> changeObjToDo(List<Object[]> listObj){
		List<ThingHandoverCongfigDO> thingHandoverCongfigList = new ArrayList<ThingHandoverCongfigDO>();
		for(Object[] objects : listObj){
			ThingHandoverCongfigDO thingHandoverCongfigDO = new ThingHandoverCongfigDO();
			thingHandoverCongfigDO.setKeyword(objects[0]==null?null:new Long(objects[0].toString()));
			thingHandoverCongfigDO.setThingHandoverCongfigName(objects[1]==null?null:objects[1].toString());
			thingHandoverCongfigDO.setDescription(objects[2]==null?null:objects[2].toString());
			thingHandoverCongfigDO.setSequence(objects[3]==null?null:Short.parseShort(objects[3].toString()));
			thingHandoverCongfigList.add(thingHandoverCongfigDO);
		}
		return thingHandoverCongfigList;
	}

}
